package com.guli.product.service.impl;

import com.guli.product.dao.PmsAttrDao;
import com.guli.product.dao.PmsSkuImagesDao;
import com.guli.product.dao.PmsSpuInfoDescDao;
import com.guli.product.entity.*;
import com.guli.product.fegin.CouponFegin;
import com.guli.product.service.*;
import com.guli.product.to.SmsMemberPriceTo;
import com.guli.product.to.SmsSkuFullReductionTo;
import com.guli.product.to.SmsSkuLadderTo;
import com.guli.product.to.SmsSpuBoundsTo;
import com.guli.product.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.product.dao.PmsSpuInfoDao;
import org.springframework.transaction.annotation.Transactional;


@Service("pmsSpuInfoService")
public class PmsSpuInfoServiceImpl extends ServiceImpl<PmsSpuInfoDao, PmsSpuInfoEntity> implements PmsSpuInfoService {

    @SuppressWarnings("all")
    @Autowired
    PmsSpuInfoDao spuInfoDao;

    @SuppressWarnings("all")
    @Autowired
    PmsSpuInfoDescDao spuInfoDescDao;

    @SuppressWarnings("all")
    @Autowired
    PmsSkuImagesService pmsSkuImagesService;

    @SuppressWarnings("all")
    @Autowired
    PmsAttrDao pmsAttrDao;

    @SuppressWarnings("all")
    @Autowired
    PmsProductAttrValueService pmsProductAttrValueService;

    @SuppressWarnings("all")
    @Autowired
    PmsSkuInfoService pmsSkuInfoService;

    @SuppressWarnings("all")
    @Autowired
    PmsSkuImagesService imagesService;

    @SuppressWarnings("all")
    @Autowired
    PmsSkuSaleAttrValueService attrValueService;

    @Autowired
    CouponFegin couponFegin;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PmsSpuInfoEntity> page = this.page(
                new Query<PmsSpuInfoEntity>().getPage(params),
                new QueryWrapper<PmsSpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void addspu(SpuInfoVo spuInfoVo) {
        PmsSpuInfoEntity pmsSpuInfoEntity = new PmsSpuInfoEntity();
        BeanUtils.copyProperties(spuInfoVo,pmsSpuInfoEntity);
        pmsSpuInfoEntity.setCreateTime(new Date());
        pmsSpuInfoEntity.setUpdateTime(new Date());
        spuInfoDao.insert(pmsSpuInfoEntity);

        //2介绍图片添加 pmsspuinfodesc
        PmsSpuInfoDescEntity pmsSpuInfoDescEntity = new PmsSpuInfoDescEntity();
        List<String> decript = spuInfoVo.getDecript();
        if (decript!=null){
            String img="";
            for (String s : decript) {
                img+=","+s;
            }
            img.substring(1);
            //因为desc表是主键是没有自增的所以手动设置主键值 配置文件设置的自增和数据库不一致 在对应表实体类进行修改主键类型
            //前面的spuinfo表已经添加过了 主键回显了 所以用他的主键作为spuinfo表的主键
            pmsSpuInfoDescEntity.setSpuId(pmsSpuInfoEntity.getId());
            pmsSpuInfoDescEntity.setDecript(img);
        }
        spuInfoDescDao.insert(pmsSpuInfoDescEntity);


        List<String> images = spuInfoVo.getImages();
        if (images!=null){
            List<PmsSkuImagesEntity> collect = images.stream().map(url -> {
                PmsSkuImagesEntity pmsSkuImagesEntity = new PmsSkuImagesEntity();
                pmsSkuImagesEntity.setSkuId(pmsSpuInfoEntity.getId());
                pmsSkuImagesEntity.setImgUrl(url);
                return pmsSkuImagesEntity;
            }).collect(Collectors.toList());
            pmsSkuImagesService.saveBatch(collect);
        }

        List<BaseAttrs> baseAttrs = spuInfoVo.getBaseAttrs();
        if (baseAttrs!=null){
            List<PmsProductAttrValueEntity> collect = baseAttrs.stream().map(item -> {
                PmsProductAttrValueEntity pmsProductAttrValueEntity = new PmsProductAttrValueEntity();
                pmsProductAttrValueEntity.setAttrId(item.getAttrId());
                pmsProductAttrValueEntity.setAttrValue(item.getAttrValues());


                PmsAttrEntity pmsAttrEntity = pmsAttrDao.selectById(item.getAttrId());
                pmsProductAttrValueEntity.setAttrName(pmsAttrEntity.getAttrName());
                pmsProductAttrValueEntity.setQuickShow(pmsAttrEntity.getShowDesc());
                pmsProductAttrValueEntity.setSearchType(pmsAttrEntity.getSearchType());
                return pmsProductAttrValueEntity;
            }).collect(Collectors.toList());
            pmsProductAttrValueService.saveBatch(collect);
        }


        List<Skus> skus = spuInfoVo.getSkus();
        if (skus!=null){
            for (Skus skus1 : skus) {

                PmsSkuInfoEntity pmsSkuInfoEntity = new PmsSkuInfoEntity();
                List<Images> images1 = skus1.getImages();
                if (images1!=null){
                    Images images2 = images1.stream().filter(item -> {
                        return item.getDefaultImg().equals(1);
                    }).findFirst().get();
                    pmsSkuInfoEntity.setSkuDefaultImg(images2.getImgUrl());
                }
                pmsSkuInfoEntity.setSaleCount(1L);
                pmsSkuInfoEntity.setSpuId(pmsSpuInfoEntity.getId());
                pmsSkuInfoEntity.setSkuName(skus1.getSkuName());
                pmsSkuInfoEntity.setCatalogId(pmsSpuInfoEntity.getCatalogId());
                pmsSkuInfoEntity.setBrandId(pmsSpuInfoEntity.getBrandId());
                pmsSkuInfoEntity.setSkuDesc(pmsSpuInfoEntity.getSpuDescription());
                pmsSkuInfoEntity.setSkuTitle(skus1.getSkuTitle());
                pmsSkuInfoEntity.setSkuSubtitle(skus1.getSkuSubtitle());
                pmsSkuInfoEntity.setPrice(skus1.getPrice());
                pmsSkuInfoService.save(pmsSkuInfoEntity);

                List<Images> images2 = skus1.getImages();
                if (images2!=null){
                    List<PmsSkuImagesEntity> collect = images2.stream().filter(item -> {
                        return StringUtils.isNotEmpty(item.getImgUrl());
                    }).map(item->{
                        PmsSkuImagesEntity pmsSkuImagesEntity = new PmsSkuImagesEntity();
                        pmsSkuImagesEntity.setImgUrl(item.getImgUrl());
                        pmsSkuImagesEntity.setDefaultImg(item.getDefaultImg());
                        return pmsSkuImagesEntity;
                    }).collect(Collectors.toList());
                    imagesService.saveBatch(collect);
                }

                List<Attr> attr = skus1.getAttr();
                if (attr!=null){
                    List<PmsSkuSaleAttrValueEntity> collect = attr.stream().map(item -> {
                        PmsSkuSaleAttrValueEntity saleAttrValueEntity = new PmsSkuSaleAttrValueEntity();
                        saleAttrValueEntity.setAttrId(item.getAttrId());
                        saleAttrValueEntity.setAttrName(item.getAttrName());
                        saleAttrValueEntity.setAttrValue(item.getAttrValue());
                        PmsAttrEntity pmsAttrEntity = pmsAttrDao.selectById(item.getAttrId());
                        saleAttrValueEntity.setSearchType(pmsAttrEntity.getSearchType());
                        saleAttrValueEntity.setSkuId(pmsSkuInfoEntity.getSkuId());
                        return saleAttrValueEntity;
                    }).collect(Collectors.toList());
                    attrValueService.saveBatch(collect);
                }

                SmsSkuLadderTo smsSkuLadderTo = new SmsSkuLadderTo();
                smsSkuLadderTo.setSkuId(pmsSkuInfoEntity.getSkuId());
                smsSkuLadderTo.setAddOther(skus1.getCountStatus());
                smsSkuLadderTo.setPrice(skus1.getPrice());
                smsSkuLadderTo.setDiscount(skus1.getDiscount());
                smsSkuLadderTo.setFullCount(skus1.getFullCount());
                couponFegin.save(smsSkuLadderTo);

                SmsSkuFullReductionTo fullReductionTo = new SmsSkuFullReductionTo();
                fullReductionTo.setFullPrice(skus1.getFullPrice());
                fullReductionTo.setReducePrice(skus1.getReducePrice());
                fullReductionTo.setSkuId(smsSkuLadderTo.getSkuId());
                fullReductionTo.setAddOther(skus1.getCountStatus());
                couponFegin.saveFullReduction(fullReductionTo);

                List<MemberPrice> memberPrice = skus1.getMemberPrice();
                if (memberPrice!=null){
                    List<SmsMemberPriceTo> collect = memberPrice.stream().map(item -> {
                        SmsMemberPriceTo smsMemberPriceTo = new SmsMemberPriceTo();
                        smsMemberPriceTo.setMemberPrice(item.getPrice());
                        smsMemberPriceTo.setSkuId(pmsSkuInfoEntity.getSkuId());
                        smsMemberPriceTo.setMemberLevelId(item.getId());
                        smsMemberPriceTo.setMemberLevelName(item.getName());
                        return smsMemberPriceTo;
                    }).collect(Collectors.toList());
                    couponFegin.saveBathMemberPrice(collect);
                }
            }

            Bounds bounds = spuInfoVo.getBounds();
            if (bounds!=null){
                SmsSpuBoundsTo smsSpuBoundsTo = new SmsSpuBoundsTo();
                smsSpuBoundsTo.setSpuId(pmsSpuInfoEntity.getId());
                smsSpuBoundsTo.setGrowBounds(bounds.getGrowBounds());
                smsSpuBoundsTo.setBuyBounds(bounds.getBuyBounds());
                couponFegin.saveBounds(smsSpuBoundsTo);
            }
        }
    }

}